﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace EP2_Gameserver
{
    public static class Protocols
    {
        public static void Received_Login()
        {
            string UserName = Program.bytes.ReadString(23);
            string Password = Program.bytes.ReadString(23 + UserName.Length + 1);

            if ((Program.userid = mySQL.VerifyLogin(UserName, Password)) != 0)
            {
                int CharCount = 0;


                string CharIDs = mySQL.GetCharIDs(Program.userid);
                string[] IdArray = CharIDs.Split('-');

                if (IdArray.Length != 0)
                {
                    for (int a = 0; a <= IdArray.Length - 1; a++)
                    {
                        try
                        {
                            int charID = Convert.ToInt32(IdArray[a]);
                            if (charID > 0)
                            {
                                CharCount++;
                                if (CharCount < 5)
                                {
                                    Program.stream.SendPack(Packages.ShowCharacters(charID));
                                    LogScreen.Info("Send CharInfo USER=" + UserName + " CHAR: " + charID.ToString());


                                }
                            }
                        }
                        catch { }
                    }
                }

                Program.stream.SendPack(Packages.GoCharSelectScreen());

            }
        }

        public static void Received_CharScreen_CharSelected()
        {
            Program.stream.SendPack(Packages.CharSelectedLoadMap());
        }

        public static void Received_WorldLoaded()
        {

            Program.stream.SendPack(Packages.EnterWorldPack1());
            Program.stream.SendPack(Packages.GameTime());
            Program.stream.SendPack(Packages.ThisPlayerGuildInfo());
            Program.stream.SendPack(Packages.ThisPlayerGuildMembers());
            Program.stream.SendPack(Packages.ThisPlayerInfo());

            Data.SpawnedIndex[861] = new ArrayList();

        }


        public static void Received_CharacterCreated()
        {
            string CharName = Program.bytes.ReadString(19);
            byte CharClass = Program.bytes[19 + CharName.Length + 1];
            byte CharHair = Program.bytes[19 + CharName.Length + 2];
            byte CharFace = Program.bytes[19 + CharName.Length + 3];

            LogScreen.Info("Character creation : " + CharName);
            mySQL.CreateCharacter(Program.userid, CharName, CharClass, CharHair, CharFace);

            int CharCount = 0;

            string CharIDs = mySQL.GetCharIDs(Program.userid);
            string[] IdArray = CharIDs.Split('-');

            if (IdArray.Length != 0)
            {
                for (int a = 0; a <= IdArray.Length - 1; a++)
                {
                    try
                    {
                        int charID = Convert.ToInt32(IdArray[a]);
                        if (charID > 0)
                        {
                            CharCount++;
                            if (CharCount < 5)
                            {
                                Program.stream.SendPack(Packages.ShowCharacters(charID));

                            }
                        }
                    }
                    catch
                    { }

                }
            }

            Program.stream.SendPack(Packages.GoCharSelectScreen());

        }

        public static void SpawnNearNPCs(int CharID)
        {

            float Min_X = Data.CurrentPos_X[CharID] - 100;
            float Max_X = Data.CurrentPos_X[CharID] + 100;

            float Min_Z = Data.CurrentPos_Z[CharID] - 100;
            float Max_Z = Data.CurrentPos_Z[CharID] + 100;

            for (int a = 0; a <= Data.SpawnList.Rows.Count - 1; a++)
            {

                DataRow row = Data.SpawnList.Rows[a];

                int NPC = Convert.ToInt32(row["NPC"]);
                int SpawnID = Convert.ToInt32(row["ID"]);
                float PosX = Convert.ToSingle(row["X"]);
                float PosZ = Convert.ToSingle(row["Z"]);
                float PosH = Convert.ToSingle(row["H"]);
                float PosR = Convert.ToSingle(row["R"]);
                if (Data.SpawnedIndex[CharID].Contains(SpawnID))
                {
                }
                else
                {
                    if (PosX > Min_X && PosX < Max_X && PosZ > Min_Z && PosZ < Max_Z)
                    {
                        Data.SpawnedIndex[CharID].Add(SpawnID);
                        Program.stream.SendPack(Packages.SpawnNPC(NPC, PosX, PosZ, PosH, PosR));
                    }
                }

            }

        }

        public static void Received_CharacterMoving()
        {

            byte[] raw_CharID = new byte[4];
            byte[] raw_PosX = new byte[4];
            byte[] raw_PosZ = new byte[4];

            Array.Copy(Program.bytes, 20, raw_CharID, 0, 4);
            Array.Copy(Program.bytes, 28, raw_PosX, 0, 4);
            Array.Copy(Program.bytes, 32, raw_PosZ, 0, 4);

            int CharID = BitConverter.ToInt32(raw_CharID.Reverse().ToArray(),0);

            Data.CurrentPos_X[CharID] = BitConverter.ToSingle(raw_PosX, 0);
            Data.CurrentPos_Z[CharID] = BitConverter.ToSingle(raw_PosZ, 0);

            Protocols.SpawnNearNPCs(CharID);
            
        }

        public static void Received_CharacterStopMoving()
        {
            byte[] raw_CharID = new byte[4];
            byte[] raw_PosX = new byte[4];
            byte[] raw_PosZ = new byte[4];

            Array.Copy(Program.bytes, 20, raw_CharID, 0, 4);
            Array.Copy(Program.bytes, 28, raw_PosX, 0, 4);
            Array.Copy(Program.bytes, 32, raw_PosZ, 0, 4);

            int CharID = BitConverter.ToInt32(raw_CharID.Reverse().ToArray(), 0);

            Data.CurrentPos_X[CharID] = BitConverter.ToSingle(raw_PosX, 0);
            Data.CurrentPos_Z[CharID] = BitConverter.ToSingle(raw_PosZ, 0);

            LogScreen.Info("Char: " + CharID + " Location : " + Data.CurrentPos_X[CharID].ToString() + " , " + Data.CurrentPos_Z[CharID].ToString());

            Protocols.SpawnNearNPCs(CharID);
            
        }

        public static void Reveived_ChatMessage()
        {
            byte MsgType = Program.bytes[18];
            int CharID = BitConverter.ToInt32(Program.bytes, 19);
            string CharName = Program.bytes.ReadString(23);
            string Message = Program.bytes.ReadString((23 + CharName.Length + 2));
            int NoIdea = BitConverter.ToInt32(Program.bytes, (23 + CharName.Length + 2 + Message.Length + 1));

            Program.stream.SendPack(Packages.SendChatMessage(CharID, CharName, Message , MsgType));

        }


    }
}
 